import re
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
name=[]
class Albb:
    def login(self):
        driver = webdriver.Chrome()  # 创建浏览器驱动对象
        driver.maximize_window()  # 窗口最大化
        driver.get("https://login.1688.com/member/signin.htm")   # 打开首页面
        driver.implicitly_wait(10)  # 隐式等待
        content=driver.page_source
        pattern = r'tpCreditUrl":"(https://.*?\.1688\.com)'
        pattern1 = r'https://(.*?).1688.com'
        urls = re.findall(pattern, content)
        url_list=[]
        for i in urls:
            resp = re.findall(pattern1, i)
            print(resp)
            url_list.extend(resp)
        print(url_list)
        for x in url_list:
            driver.execute_script("window.open();")
            all_handles = driver.window_handles
            new_window = [handle for handle in all_handles if handle != driver.current_window_handle][0]
            driver.switch_to.window(new_window)
            driver.get('https://{}.1688.com/page/contactinfo.htm?spm=0.0.wp_pc_common_topnav_38229151.0'.format(x))
            time.sleep(2)
            merchant_name_path='//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[2]'
            merchant_name=driver.find_element(By.XPATH,merchant_name_path).text
            name.append(merchant_name)
            driver.close()
resp=Albb()
resp.login()